package com.zy.pro.notice.web;

import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.notice.model.Notice;
import com.zy.pro.notice.service.NoticeService;
import com.zy.pro.notice.web.form.NoticeSearchForm;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.web.form.ShopSearchForm;
import com.zy.utils.UploadHelper;
/**
 * 公告管理控制器
 *
 */
@Controller
@RequestMapping("/control/notice/noticeController")
public class NoticeController extends BaseController {

	private static final Logger logger = Logger.getLogger(NoticeController.class);
	
	/**
	 * 公告管理服务类
	 */
	@Resource(name = "noticeServiceImpl")
	private NoticeService noticeService;
	
	/**
	 * 系统-列出公告管理
	 */
	@RequestMapping("/sysNoticeList")
	public String sysNoticeList(NoticeSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			if(form.getNoticeTitle() != null && form.getNoticeTitle() != ""){
				form.setNoticeTitle(form.getNoticeTitle().trim());
			}
			
			form.getOrderby().put("noticeCreateTime", "desc");
			
			//创建页面对象
			PageView<Notice> pageView = new PageView<Notice>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(noticeService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/notice/noticeController/sysNoticeList.do");
			//TODO 转发页面
			return "control/system/notice/sysNoticeList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑公告状态
	 */
	@RequestMapping("/editStatus")
	public void editStatus(NoticeSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			Notice s = noticeService.find(form.getNoticeId());
			if(null!=s){
				Notice notice = new Notice();
				notice.setNoticeId(form.getNoticeId());
				notice.setNoticeStatus(form.getNoticeStatus());//1:正常,2:冻结,3:删除
				noticeService.updateBySelective(notice);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	/**
	 * 系统-显示添加公告管理
	 */
	@RequestMapping("/dispalySysAdd")
	public String dispalySysAdd(Notice notice, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			map.addAttribute("operateStatus", "sysAdd");
			map.addAttribute("notice", notice);
			//TODO 转发页面
			return "/control/system/notice/sysEditNotice";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-显示编辑公告管理
	 */
	@RequestMapping("/dispalySysEdit")
	public String dispalySysEdit(Notice notice, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			notice = noticeService.find(notice.getNoticeId());
			map.addAttribute("operateStatus", "sysEdit");
			map.addAttribute("notice", notice);
			//TODO 转发页面
			return "/control/system/notice/sysEditNotice";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	

	/**
	 * 系统-添加公告管理
	 */
	@RequestMapping("/sysAdd")
	public String sysAdd(Notice notice, ModelMap map) {
		try {
			//TODO 设置shop  eg:主键信息
			notice.setNoticeStatus(1);
			notice.setNoticeCreateTime(new Date());
			noticeService.save(notice);
			return sysNoticeList(new NoticeSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 系统-编辑公告管理
	 */
	@RequestMapping("/sysEdit")
	public String sysEdit(Notice notice,ModelMap map, HttpServletRequest request) {
		try {
			//TODO:处理店铺管理
			
			noticeService.updateBySelective(notice);
			return sysNoticeList(new NoticeSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 列出公告管理
	 */
	@RequestMapping("/list")
	public String list(NoticeSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<Notice> pageView = new PageView<Notice>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(noticeService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示添加公告管理
	 */
	@RequestMapping("/displayAdd")
	public String displayAdd(Notice notice, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 添加公告管理
	 */
	@RequestMapping("/add")
	public String add(Notice notice, ModelMap map) {
		try {
			//TODO 设置notice  eg:主键信息
			
			noticeService.save(notice);
			return list(new NoticeSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 显示编辑公告管理
	 */
	@RequestMapping("/displayEdit")
	public String displayEdit(NoticeSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:Notice notice = noticeService.find(form.getId());
			//eg:map.addAttribute("entity", notice);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑公告管理
	 */
	@RequestMapping("/edit")
	public String edit(Notice Notice, ModelMap map) {
		try {
			//TODO:处理公告管理
			
			
			noticeService.updateBySelective(Notice);
			return list(new NoticeSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	/**
	 * 删除单个公告管理
	 */
	@RequestMapping("/deletenotice")
	public String delete(NoticeSearchForm form, ModelMap map) {
		try {
			//TODO  eg:noticeService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 批量删除公告管理
	 */
	@RequestMapping("/batchDelete")
	public String deletes(NoticeSearchForm form, ModelMap map) {
		try {
//			TODO  eg:noticeService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
}
